<template>
	<view class="exchange-page" v-if="orderDetail">
		<view class="exchange-info">
			<image :src="orderDetail.image" class="item-left"></image>
			<view class="item-center">
				<view class="item-title">{{orderDetail.name}}</view>
				<view class="item-time" v-if="orderDetail.pay_type==1">购买时间：{{orderDetail.createtime}}</view>
				<view class="item-time" v-if="orderDetail.pay_type==2">兑换时间：{{orderDetail.createtime}}</view>
			</view>
			<view class="item-right">
				<view class="score" v-if="orderDetail.pay_type==1">￥{{orderDetail.price}}</view>
				<view class="score" v-if="orderDetail.pay_type==2">{{orderDetail.price}}积分</view>
				<view class="button button1" v-if="orderDetail.status==1&&orderDetail.pay_type==1"
					@click.stop="orderRefund">
					申请退款</view>
				<view class="button button1" v-if="orderDetail.status==1&&orderDetail.pay_type==2"
					@click.stop="orderRefund">
					取消兑换</view>
				<view class="button button2" v-if="orderDetail.status==2">进行中</view>
				<view class="button button2" v-if="orderDetail.status==3">申请中</view>
				<view class="button button3" v-if="orderDetail.status==4">已退款</view>
				<view class="button button3" v-if="orderDetail.status==5">已完成</view>
			</view>
		</view>
		<view class="order-detail">
			<view class="title">订单详情</view>
			<view class="order-item">
				<view>订单编号</view>
				<view>{{orderDetail.out_trade_no}}</view>
			</view>
			<view class="order-item">
				<view>订单金额</view>
				<view v-if="orderDetail.pay_type==1">￥{{orderDetail.price}}</view>
				<view v-if="orderDetail.pay_type==2">{{orderDetail.price}}积分</view>
			</view>
			<view class="order-item" v-if="orderDetail.coupon_money">
				<view>优惠减免</view>
				<view v-if="orderDetail.pay_type==1">- ￥{{orderDetail.coupon_money}}</view>
				<view v-if="orderDetail.pay_type==2">- {{orderDetail.coupon_money}}积分</view>
			</view>
			<view class="order-item">
				<view>实付金额</view>
				<view v-if="orderDetail.pay_type==1">￥{{orderDetail.pay_money}}</view>
				<view v-if="orderDetail.pay_type==2">{{orderDetail.pay_money}}积分</view>
			</view>
		</view>
		<view class="order-detail" v-if="pageType == 'goods'&&orderDetail.image_url">
			<view class="title">核销码</view>
			<view class="image-box">
				<image :src="orderDetail.image_url" class="image"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageType: null,
				order_id: null,
				orderDetail: null,
			}
		},
		onLoad(options) {
			if (options && options.page) {
				this.pageType = options.page
			}
			if (options && options.order_id) {
				this.order_id = options.order_id
				this.getOrderDetail()
			}
		},
		methods: {
			orderRefund() {
				let that = this
				uni.showModal({
					title: "提示",
					content: "是否取消该商品的兑换/购买",
					success(res) {
						if (res.confirm) {
							if (that.pageType == 'goods') {
								that.$Api.user.goodsOrderRefund({
									order_id: that.order_id,
								}).then(res => {
									that.$Toast('审核中，请稍后')
									that.getOrderDetail()
								})
							} else {
								that.$Api.user.courseOrderRefund({
									order_id: that.order_id,
								}).then(res => {
									that.$Toast('审核中，请稍后')
									that.getOrderDetail()
								})
							}
						}
					}
				})
			},
			getOrderDetail() {
				if (this.pageType == 'goods') {
					this.$Api.user.goodsOrderDetail({
						order_id: this.order_id,
					}).then(res => {
						this.orderDetail = res.data
					})
				} else {
					this.$Api.user.courseOrderDetail({
						order_id: this.order_id,
					}).then(res => {
						this.orderDetail = res.data
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.exchange-page {
		width: 100%;
		min-height: 100vh;
		background: #F6F6F8;
		padding-top: 20rpx;
		box-sizing: border-box;

		.exchange-info {
			margin: 0rpx 30rpx 20rpx;
			border-radius: 28rpx;
			padding: 20rpx;
			display: flex;
			align-items: center;
			background: #ffffff;
			box-shadow: 0rpx 0rpx 12rpx 0rpx rgba(0, 0, 0, 0.1), inset 0rpx 2rpx 2rpx 0rpx #FFFFFF;

			.item-left {
				width: 156rpx;
				height: 156rpx;
				margin-right: 26rpx;
			}

			.item-center {
				flex: 1;
				min-height: 156rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.item-title {
					font-weight: bold;
					font-size: 32rpx;
				}

				.exchange-already {
					font-size: 26rpx;
					color: #565656;
					margin-top: 6rpx;
				}

				.item-time {
					font-size: 26rpx;
					color: #565656;
					margin-top: 16rpx;
				}
			}

			.item-right {
				min-width: 200rpx;
				min-height: 156rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: flex-end;

				.score {
					font-size: 36rpx;
					color: #FD463E;
					text-align: right;
				}

				.button {
					width: 156rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					border-radius: 44rpx;
				}

				.button1 {
					color: #FFB900;
					background: #FFF8E5;
					border: 1rpx solid #FFB900;
				}

				.button2 {
					color: #A3C616;
					background: #FBFFED;
					border: 1rpx solid #A3C616;
				}

				.button3 {
					color: #DADADA;
					background: #F9F9F9;
					border: 1rpx solid #E5E5E5;
				}
			}
		}

		.order-detail {
			background: #ffffff;
			border-radius: 28rpx;
			margin: 0rpx 30rpx 20rpx;
			padding: 30rpx 26rpx;
			box-sizing: border-box;
			
			.title {
				font-size: 32rpx;
				border-bottom: 1px #e6e5e5 solid;
				padding-bottom: 28rpx;
				font-weight: bold;
			}
			.image-box{
				display: flex;
				justify-content: center;
				align-items: center;
				margin-top: 20rpx;
				.image{
					width: 296rpx;
					height: 296rpx;
				}
			}
			.order-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
				margin-top: 26rpx;
			}
		}
	}
</style>